package cn.lee.demo.services;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import cn.lee.demo.receiver.IncomingCallReceiver;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-4-25
 * Time: 下午2:13
 */
public class IncomingCallReceiverService extends Service {
    private static final String TAG="Cloud-Manager";
    private IncomingCallReceiver mIncomingCallReceiver;
    private boolean isRegiset = false;
    private static final String ACTION = "android.intent.action.PHONE_STATE";
    @Override
    public IBinder onBind(Intent intent) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mIncomingCallReceiver = new IncomingCallReceiver();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.v(TAG, "启动服务");
        regiset();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v(TAG,"销毁服务");
        unregiset();
    }
    public void regiset() {
        IntentFilter filter = new IntentFilter(ACTION);
        filter.setPriority(1000);//设置优先级最大
        isRegiset = true;
        registerReceiver(mIncomingCallReceiver,filter);
    }

    public void unregiset() {
        if (mIncomingCallReceiver != null && isRegiset) {
            unregisterReceiver(mIncomingCallReceiver);
            isRegiset = false;
            Log.v(TAG,"取消注册");
        } else
            Log.v(TAG,"未注册");
    }
}
